#ifndef Scenic_Renderer_Tesselator_h
#define Scenic_Renderer_Tesselator_h


#include "common.h"
#include <GL/glu.h>



namespace Scenic 
{ 
namespace Renderer
{

#define TESS_BLOCK_SIZE 100
 
struct TessVertex
{
	double p[3];
	int id;
};
 
struct TessBlock
{
	struct TessVertex vertices[TESS_BLOCK_SIZE];
	struct TessBlock *next;
};

class TessVertexBuffer
{
	TessBlock *tessBlocks;
	int numTessVertex;
	int vertexCount;

public:
	TessVertexBuffer();
	~TessVertexBuffer();

	TessVertex *newVertex();
	void free();
};

class Tesselator : public Resource
{
	GLUtesselator *tessObj;
	TessVertexBuffer buffer;
	int vertices[3];
	bool edges[3];
	int vertexCount;
	bool edge;

public:
	Tesselator();

	void begin();
	void end();
	void beginContour();
	void endContour();
	void vertex(double x, double y);

	virtual void addVertex(double x, double y) = 0;
	virtual void addTriangle(int vertex1, bool edge1, int vertex2, bool edge2, int vertex3, bool edge3) = 0;

	void edgeFlag(bool edge);
	void combineFunc(double coords[3], struct TessVertex *d[4], float w[4], struct TessVertex **dataOut);
	void vertexFunc(struct TessVertex *data);

	static Tesselator *get(int id);
};

}
}

#endif
